# Maintainer: Sven-Hendrik Haase <svenstaro@gmail.com>

# NOTE: Gitlab isn't always compatible with modern Ruby versions. In that case, check the
# commit log for an old fix on how to tell it to use older versions of Ruby. I'm afraid we'll
# need this again at some point in the future.
pkgname=gitlab-gitaly
pkgver=15.11.0
pkgrel=1
pkgdesc="Speed up Git access using caching"
arch=('loong64' 'x86_64')
url="https://gitlab.com/gitlab-org/gitaly"
license=('MIT')
depends=(glibc ruby2.7 libxml2 libxslt libssh2)
options=(!buildflags)
makedepends=(go cmake git)
backup=("etc/gitlab-gitaly/config.toml")
_tag=v${pkgver}
source=("https://gitlab.com/gitlab-org/gitaly/-/archive/${_tag}/gitaly-${_tag}.tar.gz"
        "configs.patch"
        "gitlab-gitaly.service"
	"ruby27.patch")
sha512sums=('49b04cfdb3eed5cdaaa02228233a383cec12b9d716fdd3a742168714e784653579538bb40742f7db09e9ca9e9b7c23a073a116ebb86c84de46f107e1274487da'
            '9b824a791c7f563857e99a7faad013d50eb1ca93d49089742dd7ccdfbb325c1c4a48db4dc83820fda5dfedfbd3e2b03c786c8fa0f2eb3c4b78ce3bbeefb97584'
            '7f5cd528c873a5e43e18aa6a88bd7298422c047e0e61cf3208be7d9fcfdfdc8a844b5c439ab6afc2098c5c4c60ed9c3d167c2f87517f1e93b34f39be3d3dad09'
            '2e4171e42cf7d5be91ef58e720b53159c1cdc4cb6932e9044094a99e9ee21ee8792067c0f539e11336c59748ea03291c069ff265cdaf5b4aea4b14f6d0929bd8')

prepare() {
  cd gitaly-$_tag

  patch -p1 < ../ruby27.patch

  patch -p1 < ../configs.patch
  # At this point the config file should not contain any references to '/home/git'

  # https://github.com/bundler/bundler/issues/6882
  sed -e '/BUNDLED WITH/,+1d' -i ruby/Gemfile.lock
}

build() {
  set -x
  cd gitaly-$_tag

  pushd ruby
  gem-2.7 sources -r https://rubygems.org/
  gem-2.7 sources -a https://rubygems.loongnix.cn
  gem-2.7 sources -c
#  bundle-2.7 config force_ruby_platform true # build from sources as some prebuilt gems are not available for newer ruby
#  bundle-2.7 install --path vendor/bundle
  popd

  make V=1 BUILD_TAGS="tracer_static tracer_static_jaeger"
}

package() {
  cd gitaly-$_tag

  make PREFIX=/usr DESTDIR="${pkgdir}" install
  mkdir -p "${pkgdir}"/etc/gitlab-gitaly
  mkdir -p "${pkgdir}"/usr/share/webapps/gitlab-gitaly
  cp -r ruby "${pkgdir}"/usr/share/webapps/gitlab-gitaly/ruby

  install -Dm644 config.toml.example "${pkgdir}"/etc/${pkgname}/config.toml
  install -Dm644 "LICENSE" "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
  install -Dm644 "${srcdir}"/gitlab-gitaly.service "${pkgdir}"/usr/lib/systemd/system/gitlab-gitaly.service
}
